package com.joinlabs.action;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.joinlabs.entity.Forwarding;
import com.joinlabs.entity.Img;
import com.joinlabs.entity.Log;
import com.joinlabs.entity.MsgOfZan;
import com.joinlabs.entity.PublishBean;
import com.joinlabs.entity.PublishLogBean;
import com.joinlabs.entity.Topic;
import com.joinlabs.entity.Type;
import com.joinlabs.entity.UploadRes;
import com.joinlabs.entity.User;
import com.joinlabs.entity.Weibo;
import com.joinlabs.service.IForwardService;
import com.joinlabs.service.IImgService;
import com.joinlabs.service.ILogService;
import com.joinlabs.service.IMsgOfZanService;
import com.joinlabs.service.ITopicService;
import com.joinlabs.service.IUserService;
import com.joinlabs.service.IWeiboService;
import com.joinlabs.service.impl.TopicServiceImpl;

@Controller
@RequestMapping("/friend")
@SessionAttributes(names = { "user", "originalList", "imgWeiboList", "logLists", "weiboLists" }) // 声明要操作session域中的 多个
																									// 域对象的名字
public class FriendCycleAction {
	@Resource
	private IUserService userService;// 用户
	@Resource
	private ITopicService topicService;// 话题
	@Resource
	private IWeiboService weiboService;// 微博
	@Resource
	private IForwardService forwardService;// 转发
	@Resource
	private ILogService logService;// 日志
	@Resource
	private IImgService imgService;//相册
	@Resource
	private IMsgOfZanService msgOfZanService;//点赞消息

	@RequestMapping("/index")
	public ModelAndView showindex(ModelMap map) {
		// 模拟一个登录成功的用户
//		User user = userService.selectByPrimaryKey((long) 1);
//		System.out.println(user);
//		// 添加到session域中
//		map.addAttribute("user", user);
		//获取当前用户
		User user = (User) map.get("user");
		// 查出全部微博
		List<Weibo> weiboList = weiboService.selectAllWeibo();
		System.out.println("---------"+weiboList.size());
		map.addAttribute("weiboList", weiboList);
		// 查询所有转发微博
		List<Forwarding> forwardList = forwardService.selectAllForward();
		map.addAttribute("forwardList", forwardList);
		System.out.println(forwardList);
		// 查询所有日志
		List<Log> logList = logService.selectAllLog();
		map.addAttribute("logList", logList);
		// 显示热门话题
		List<Topic> topicList = topicService.selectRecentTopic();
		map.addAttribute("topicList", topicList);
		//查询该用户所有点赞微博的id
		List<MsgOfZan> zanList = msgOfZanService.selectZanByUserId(user.getUserId());
		map.addAttribute("zanList", zanList);
		// 跳转到朋友圈界面并显示

		return new ModelAndView("/friendcycle/friend");
	}

	@RequestMapping("/original")
	@ResponseBody
	public Object showOriginal(ModelMap map) {

		// 查询原创微博
		List<Weibo> originalList = weiboService.selectAllWeibo();
		map.addAttribute("originalList", originalList);

		// 跳转到朋友圈界面并显示
		return new String("success");
	}

	@RequestMapping("/img")
	@ResponseBody
	public Object showImg(ModelMap map) {

		// 查询图片微博
		List<Weibo> imgWeiboList = weiboService.selectImgWeibo();
		map.addAttribute("imgWeiboList", imgWeiboList);

		// 跳转到朋友圈界面并显示
		return new String("success");
	}

	@RequestMapping("/article")
	@ResponseBody
	public Object showLog(ModelMap map) {

		// 查询文章
		List<Log> logLists = logService.selectAllLog();
		map.addAttribute("logLists", logLists);
		System.out.println(logLists);
		// 跳转到朋友圈界面并显示
		return new String("success");
	}

	@RequestMapping("/friendCycle")
	@ResponseBody
	public Object friendCycle(String userId, ModelMap map) {
		Long id = Long.parseLong(userId);
		// 查询朋友圈
		List<Weibo> weiboLists = weiboService.selectFriendWeibo(id);
		map.addAttribute("weiboLists", weiboLists);
		// 跳转到朋友圈界面并显示
		return new String("success");
	}

	@RequestMapping("/funny")
	@ResponseBody
	public Object funny(String flag, ModelMap map) {

		List<Weibo> weiboLists = weiboService.selectWeiboByType(flag);
		map.addAttribute("weiboLists", weiboLists);
		// 跳转到朋友圈界面并显示
		return new String("success");
	}

	// 朋友圈多图片上传
	@RequestMapping("/upload")
	@ResponseBody
	public Object upload(MultipartFile file) {
		UploadRes res = new UploadRes();
		try {
			String fileName = file.getOriginalFilename();
			String src = "/img/photo/" + fileName;
			res.setCode("1");
			res.setMsg("上传成功");
			res.setSrc(src);
			System.out.println(fileName);
			file.transferTo(new File("c://server/img/photo/" + fileName));
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return res;
	}

	// 发布微博
	@RequestMapping("/publish")
	@ResponseBody
	public Object publish(PublishBean publish, ModelMap map,HttpSession session) {
		Weibo weibo = new Weibo();// 创建一个微博Bean
		System.out.println(publish);
		System.out.println(map.get("user"));
		// 获取用户
		User user = (User) session.getAttribute("user");
		// 获取微博内容
		String content = publish.getWeibiContent();
		System.out.println(content);
		if(content.contains("#")) {
			String content2 = content.substring(content.indexOf("#"), content.lastIndexOf("#")+1);
			content = content.replaceAll(content2, "");
			System.out.println(content);
		}
		
		// 获取发布时间
		Date date = new Date();
		// 获取权限
		String weiboJurisdiction = publish.getWeiboJurisdiction();
		// 获取来源
		String source = "weibo.com";
		// 获取类别id
		Type type = new Type();
		type.setTypeId((long) 1);
		// 获取 话题并插入话题表
		Topic topic = new Topic();
		// 封装topic
		topic.setTopicId((long) 0);
		topic.setTopicName(publish.getTopicName());
		// 调用topicService插入一个话题
		topicService.insertSelective(topic);		
		// 封装微博
		weibo.setUser(user);
		weibo.setType(type);
		weibo.setTopic(topic);
		weibo.setWeiboContent(content);
		weibo.setWeiboCollectnum((long) 0);
		weibo.setWeiboCommentnum((long) 0);
		weibo.setWeiboForwardnum((long) 0);
		weibo.setWeiboId((long) 0);
		weibo.setWeiboJurisdiction(weiboJurisdiction);
		weibo.setWeiboSource(source);
		weibo.setWeiboTime(date);
		weibo.setWeiboZannum((long) 0);
		// 调用方法插入微博
		weiboService.publishWeibo(weibo);	
		//调用方法使当前用户微博数+1
		userService.updateWeiboNum(user.getUserId());
		user.setUserWbnum(user.getUserWbnum()+1);
		map.addAttribute("user", user);
		// 获取imgList并插入img表
		List<Img> imgList = new ArrayList<>();
		List<String> imgsrc = publish.getUploadImg();
		if (imgsrc == null) {
			Img img = new Img();
			img.setImgId((long) 0);
			img.setImgPath("");
			img.setWeiboId(weibo.getWeiboId());
			// 调用service 方法插入一个图片
			imgService.insertSelective(img);
			// 添加到list
			imgList.add(img);
		}else {
			for (String src : imgsrc) {
				Img img = new Img();
				img.setImgId((long) 0);
				img.setImgPath(src);
				img.setWeiboId(weibo.getWeiboId());
				// 调用service 方法插入一个图片
				imgService.insertSelective(img);
				// 添加到list
				imgList.add(img);
			}
			weibo.setImgList(imgList);			
		}
		// 查出全部微博
				List<Weibo> weiboList = weiboService.selectAllWeibo();
				System.out.println(weiboList.size()+"*********************");
				map.addAttribute("weiboList", weiboList);
		return "success";
	}
	//日志封面图片上传
	@RequestMapping("/uploadlog")
	@ResponseBody
	public Object uploadlog(MultipartFile file) {
		UploadRes res = new UploadRes();
		try {
			String fileName = file.getOriginalFilename();
			String src = "/img/article/" + fileName;
			res.setCode("1");
			res.setMsg("上传成功");
			res.setSrc(src);
			System.out.println(fileName);
			file.transferTo(new File("c://server/img/article/" + fileName));
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return res;
	}
	//发布文章
	@RequestMapping("/publishLog")
	@ResponseBody
	public Object publishLog(PublishLogBean publishLog, ModelMap map) {
		System.out.println(publishLog);
		//获取用户
		User user = (User) map.get("user");
		//获取标题
		String titleName = publishLog.getTitleName();
		//获取封面图片路径
		String src = publishLog.getLogImgPath();
		//获取日志内容
		String content = publishLog.getLogContent();
		//封装文章实体
		Log log = new Log();
		log.setAuthorName(user.getUserName());
		log.setLogContent(content);
		log.setLogId((long)0);
		log.setLogImgpath(src);
		log.setLogSource("weibo.com");
		log.setLogTime(new Date());
		log.setLogType("头条");
		log.setUser(user);
		log.setLogTitle(titleName);
		log.setLogSharenum((long)0);
		//调用service方法插入一条日志
		int row = logService.insertSelective(log);
		return "success";
	}
	//点赞
	@RequestMapping("/zan")
	@ResponseBody
	public Object zan(Long userId,Long weiboId) {
		MsgOfZan zan = new MsgOfZan();
		zan.setUserId(userId);
		zan.setWeiboId(weiboId);
		zan.setMsgofzanId((long)0);
		msgOfZanService.insertSelective(zan);
		weiboService.updateZan(weiboId);
		return "success";
	}
	//取消点赞
	@RequestMapping("/diszan")
	@ResponseBody
	public Object diszan(Long userId,Long weiboId) {
		MsgOfZan zan = new MsgOfZan();
		zan.setUserId(userId);
		zan.setWeiboId(weiboId);
		zan.setMsgofzanId((long)0);
		msgOfZanService.deleteZan(zan);
		weiboService.disupdateZan(weiboId);
		return "success";
	}
}
